<template>
  <el-card>
    <div slot="header">
      <el-button type="primary" icon="el-icon-plus" @click="showAdd">添加</el-button>
      <template>
       <el-table
        v-loading="loading"
        ref="singleTable"
        :data="trademark"
        style="width: 100%">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column
          property="tmName"
          label="品牌名称"
          width="120">
        </el-table-column>
        <el-table-column
          label="品牌Logo"
         >
         <template v-slot="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.logoUrl"></el-image>
          </template>  
        </el-table-column>
        <el-table-column 
          property="address"
          label="操作">
          <template v-slot="{row}">
            <el-button type="warning" icon="el-icon-edit" @click="updata(row)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="remove(row)">删除</el-button>
          </template>
          
        </el-table-column>
      </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[3, 5, 7, 9]"
        :page-size="pageSize"
        layout=" prev, pager, next, jumper,->,total, sizes"
        :total="total">
      </el-pagination>
    </div>
    <!-- 表单验证 -->
    <el-dialog :title="form.id?'修改品牌':'添加品牌'" :visible.sync="dialogFormVisible" >
      <el-form :model="form" label-width="100px" ref="tmForm" :rules="rules"  class="demo-ruleForm">
        <el-form-item label="品牌名称" prop="tmName">
          <el-input v-model="form.tmName"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" prop="logoUrl">
          <template>
           <el-upload  prop="logoUrl"
              class="avatar-uploader"
              action="/dev-api/admin/product/fileUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="form.logoUrl" :src="form.logoUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
           </el-upload>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="addOrupdata" :loading="query">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
  
</template>

<script>
export default {
  name: 'TrademarkList',
   data() {
      return {
        trademark: [],
        currentPage:1,
        pageSize:3,
        total:0,
        dialogFormVisible:false,//对话框默认不显示
        form:{
          tmName:"",
          logoUrl:""
        },
        loading:false,//加载中
        query:false,//确定按钮加载状态
        //表单校验
        rules:{
          tmName: [
            { required: true, message: '请输入品牌名称', trigger: 'blur' },
            { min: 2, max: 10, message: '品牌名称长度在 2 到 10 个字符', trigger: 'change' }
          ],
          logoUrl: [
            { required: true, message: '请上传品牌LOGO'},
           
          ],
        }
      }
   },
   methods: {
     //删除品牌
     async remove(row){
       await this.$api.table.remove(row.id)
       await this.getList()
       this.$message.success('操作成功')
     },
     //取消提交
     cancle(){
       this.dialogFormVisible=false
       this.$message('取消提交成功')
     },
     //提交表单
     async addOrupdata(){
       this.$refs.tmForm.validate(async (vlid)=>{
         if(vlid){
           try {
             this.dialogFormVisible=false
             this.query = true
             await this.$api.table[this.form.id?'updata':'add'](this.form)
             this.query=false
             await this.getList()
             this.$message.success('操作成功')
           } catch (error) {
             
           }
         }
       })
     },
     //点击修改按钮
      updata(row){
        this.form ={...row}
       
        this.dialogFormVisible=true
      },
      
     //点击添加按钮
     showAdd(){
       this.form={
          tmName:"",
          logoUrl:""
       }
       
       this.dialogFormVisible= true
     },
     //上传图像成功
     handleAvatarSuccess(res, file) {
        const logoUrl = res.data;
        this.form.logoUrl = logoUrl
      },
      //图像上传成功前
      beforeAvatarUpload(file) {
        const isJPGorPNG = file.type === 'image/jpeg' || file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPGorPNG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPGorPNG && isLt2M;
      },
    
     //点击添加按钮
     add(){
       this.dialogFormVisible=true
     },
     //获取分页数据
     async getList(){
       try {
         this.loading=true
         const {code,data} = await this.$api.table.getList(this.currentPage,this.pageSize)
         this.loading=false
         if(code === 200){
           
           this.trademark = data.records;
           this.total = data.total;
         }
       } catch (error) {
         throw error
       }
     },
     //修改pageSize
     async handleSizeChange(pageSize){
       this.pageSize = pageSize
       this.loading=true
       await this.getList()
       this.loading=false
     },
     //修改currentPage
     async handleCurrentChange(currentPage){
       this.currentPage = currentPage;
       this.loading=true
       await this.getList()
       this.loading=false
     }
   },
   async mounted() {
     await this.getList()

   },
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<style lang="scss" scoped>

</style>
